<?php

namespace app\logic\admin;

class L_cache
{
    /**
     * 获取位置信息
     * @param string $paramObj
     */
    public function getPositionData($paramObj = "")
    {
        $dataObj = paramsObj();
        $dataObj->request = paramsObj();
        $dataObj->data = paramsObj();
        $dataObj->data->use_trans = false;
        $dataObj->result = new \ArrayObject();

        $final_arr = logInit();
        if ($final_arr["error_no"] == 0) {
            $mixed = parseToArr($paramObj, "mixed", 0);
            $params = parseToArr($paramObj, "params", array());
            $messages = parseToArr($paramObj, "messages", array());
            $rules = array(
                'moduleId' => 'require',
            );
            $paramsObj = paramsObj();
            $paramsObj->params = $params;
            $paramsObj->mixed = $mixed;
            $paramsObj->rules = $rules;
            $paramsObj->messages = $messages;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $dataObj->request = $checkResult["result"];
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {
            $paramsObj=paramsObj();
            $paramsObj->params=array(
                "moduleId"=>$dataObj->request->moduleId,
            );
            $checkResult=app("S_Admin_article")->getPositionData($paramsObj);
            if($checkResult["error_no"]=="0"){
                $result=$checkResult["result"];
                $return=array();
                $return["start_id"]=getVal($result,"startId",0);
                $return["end_id"]=getVal($result,"endId",0);
                $return["loop_nums"]=1000;
                $final_arr["result"]=$return;
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        return $final_arr;
    }

    /**
     * 更新缓存
     * @param string $paramObj
     */
    public function update($paramObj = "")
    {
        $dataObj = paramsObj();
        $dataObj->request = paramsObj();
        $dataObj->data = paramsObj();
        $dataObj->data->use_trans = false;
        $dataObj->result = new \ArrayObject();

        $final_arr = logInit();
        if ($final_arr["error_no"] == 0) {
            $mixed = parseToArr($paramObj, "mixed", 0);
            $params = parseToArr($paramObj, "params", array());
            $messages = parseToArr($paramObj, "messages", array());
            $rules = array(
                'action' => 'require',
                'all' => '',
            );
            $dataDefault=array();
            $dataDefault["all"]=0;
            $paramsObj = paramsObj();
            $paramsObj->params = $params;
            $paramsObj->mixed = $mixed;
            $paramsObj->rules = $rules;
            $paramsObj->messages = $messages;
            $paramsObj->dataDefault = $dataDefault;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $dataObj->request = $checkResult["result"];
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {
            if($dataObj->request->action=="show"){
                $checkResult=$this->module_show($paramObj);
                if ($checkResult["error_no"] == 0) {
                    $final_arr["result"] = $checkResult["result"];
                } else {
                    $final_arr = logCallErrorMsg($final_arr, $checkResult);
                }
            }else if($dataObj->request->action=="index"){
                $checkResult=$this->module_index($paramObj);
                if ($checkResult["error_no"] == 0) {
                    $final_arr["result"] = $checkResult["result"];
                } else {
                    $final_arr = logCallErrorMsg($final_arr, $checkResult);
                }
            }else if($dataObj->request->action=="all"){
                $paramsObj=paramsObj();
                $paramsObj->params=array(
                    "update"=>"1",
                    "action"=>"show",
                    "all"=>"1",
                );
                $paramsObj=blenderObject($paramsObj,$paramObj);
                $checkResult=$this->module_show($paramsObj);
                if ($checkResult["error_no"] == 0) {
                    $final_arr["result"] = $checkResult["result"];
                } else {
                    $final_arr = logCallErrorMsg($final_arr, $checkResult);
                }
            }else if($dataObj->request->action=="list"){
                $checkResult=$this->module_list($paramObj);
                if ($checkResult["error_no"] == 0) {
                    $final_arr["result"] = $checkResult["result"];
                } else {
                    $final_arr = logCallErrorMsg($final_arr, $checkResult);
                }
            }else{
                $final_arr=recordLogMsgLevelThree(app("errorLevel")::E_WARNING,app("errorCode")::OP_FAIL,lang("sys.opFail"));
            }
        }
        return $final_arr;
    }

    /**
     * 生成首页
     * @param string $paramObj
     */
    public function module_index($paramObj = "")
    {
        $dataObj = paramsObj();
        $dataObj->request = paramsObj();
        $dataObj->data = paramsObj();
        $dataObj->data->use_trans = false;
        $dataObj->result = new \ArrayObject();

        $final_arr = logInit();
        if ($final_arr["error_no"] == 0) {
            $mixed = parseToArr($paramObj, "mixed", 0);
            $params = parseToArr($paramObj, "params", array());
            $messages = parseToArr($paramObj, "messages", array());
            $rules = array(
                'moduleId' => 'require',
                'all' => '',
            );
            $dataDefault=array();
            $paramsObj = paramsObj();
            $paramsObj->params = $params;
            $paramsObj->mixed = $mixed;
            $paramsObj->rules = $rules;
            $paramsObj->messages = $messages;
            $paramsObj->dataDefault = $dataDefault;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $dataObj->request = $checkResult["result"];
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {
            $paramsObj=paramsObj();
            $paramsObj->params=array(
                "moduleId"=>$dataObj->request->moduleId,
            );
            $checkResult=app("L_Admin_html")->module_index($paramsObj);
            if ($checkResult["error_no"] == 0) {

            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {
            $module=module($dataObj->request->moduleId);
            $params=array();
            $params["all"]=$dataObj->request->all;
            $params["moduleId"]=$dataObj->request->moduleId;

            $return=array();
            $return["params"]=$params;
            $return["progress_msg"]="{$module['name']} 首页更新成功";
            if($dataObj->request->all=="1"){
                $return["referer"] = YIFANG_ROUTE_PREFIX."/cache/update?action=list";
            }else{
                $return["referer"] = YIFANG_ROUTE_PREFIX."/cache?moduleId=".$dataObj->request->moduleId;
                $return["method"] = "get";
            }
            $final_arr["result"]=$return;
        }
        return $final_arr;
    }

    /**
     * 生成列表页
     * @param string $paramObj
     */
    public function module_list($paramObj = "")
    {
        $dataObj = paramsObj();
        $dataObj->request = paramsObj();
        $dataObj->data = paramsObj();
        $dataObj->data->use_trans = false;
        $dataObj->result = new \ArrayObject();

        $final_arr = logInit();
        if ($final_arr["error_no"] == 0) {
            $mixed = parseToArr($paramObj, "mixed", 0);
            $params = parseToArr($paramObj, "params", array());
            $messages = parseToArr($paramObj, "messages", array());
            $rules = array(
                'moduleId' => 'require',
                'all' => '',
                'categoryId' => '',
                'start_nums' => '',
                'total_nums' => '',
                'page' => '',
            );
            $dataDefault=array();
            $paramsObj = paramsObj();
            $paramsObj->params = $params;
            $paramsObj->mixed = $mixed;
            $paramsObj->rules = $rules;
            $paramsObj->messages = $messages;
            $paramsObj->dataDefault = $dataDefault;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $dataObj->request = $checkResult["result"];
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {
            $dataObj->data->pass=false;
            $index_html=setting("moduleListHtml",$dataObj->request->moduleId);
            if($index_html){
                $dataObj->data->pass=true;
            }
        }
        if ($final_arr["error_no"] == 0) {
            $module=module($dataObj->request->moduleId);
            if($dataObj->data->pass){
                $dataObj->data->temp_params=array();
                $dataObj->data->temp_return=array();
                if ($final_arr["error_no"] == 0) {
                    if(isset($dataObj->request->categoryId)){
                        //获取指定单个分类
                        $paramsObj=paramsObj();
                        $paramsObj->params=array(
                            'dataType' => 'module',
                            'dataId' => $dataObj->request->moduleId,
                            'categoryId' => $dataObj->request->categoryId,
                        );
                        $checkResult=app("S_Admin_category")->getCategoryInfo($paramsObj);
                        if ($checkResult["error_no"] == 0) {
                            $categoryResult = $checkResult["result"];
                            if($categoryResult){
                                //计算是否完成
                                $page=$dataObj->request->page?$dataObj->request->page:1;
                                $start_nums=$dataObj->request->start_nums;
                                $pagesize=20;
                                $total = max(ceil($categoryResult['item']/$pagesize), 1);
                                $num = 50;
                                if($page <= $total) {
                                    //生成一批分页数据
                                    $paramsObj=paramsObj();
                                    $paramsObj->params=array(
                                        "moduleId"=>$dataObj->request->moduleId,
                                        "page"=>$page,
                                        "nums"=>$num,
                                        "pagesize"=>$pagesize,
                                        "categoryId"=>$categoryResult['id'],
                                    );
                                    $checkResult=app("L_Admin_html")->module_list($paramsObj);
                                    if ($checkResult["error_no"] == 0) {
                                        $temp_param=array();
                                        $temp_param["total_nums"]=$dataObj->request->total_nums;
                                        $temp_param["start_nums"]=$start_nums;
                                        $temp_param["categoryId"]=$dataObj->request->categoryId;
                                        $temp_param["page"]=$page+$num;
                                        $lastPage=$page+$num-1;
                                        $temp_return=array();
                                        $temp_return["progress_msg"]="{$module['name']}[{$categoryResult['name']}] 第{$page}页至第{$lastPage}页生成成功";
                                        $temp_return["referer"] = YIFANG_ROUTE_PREFIX."/cache/update?action=list";

                                        $dataObj->data->temp_params=$temp_param;
                                        $dataObj->data->temp_return=$temp_return;
                                    } else {
                                        $final_arr = logCallErrorMsg($final_arr, $checkResult);
                                    }
                                }else{
                                    $start_nums++;
                                    $temp_param=array();
                                    $temp_param["total_nums"]=$dataObj->request->total_nums;
                                    $temp_param["start_nums"]=$start_nums;
                                    $temp_param["categoryId"]=$categoryResult['id'];

                                    $temp_return=array();
                                    $temp_return["progress_msg"]="{$module['name']}[{$categoryResult['name']}] 生成成功";
                                    $temp_return["referer"] = YIFANG_ROUTE_PREFIX."/cache/update?action=list";

                                    $dataObj->data->temp_params=$temp_param;
                                    $dataObj->data->temp_return=$temp_return;
                                }
                            }else{
                                //提示生成成功进行下一轮
                                $temp_param=array();
                                $temp_return=array();
                                $temp_return["progress_msg"]="{$module['name']}列表生成成功";
                                if($dataObj->request->all=="1"){
                                    $temp_return["referer"] = YIFANG_ROUTE_PREFIX."/cache/update?action=show";
                                }else{
                                    $temp_return["referer"] = YIFANG_ROUTE_PREFIX."/cache?moduleId=".$dataObj->request->moduleId;
                                    $temp_return["method"] = "get";
                                }
                                $dataObj->data->temp_params=$temp_param;
                                $dataObj->data->temp_return=$temp_return;
                            }
                        } else {
                            $final_arr = logCallErrorMsg($final_arr, $checkResult);
                        }
                    }else{
                        //获取模型分类数
                        $paramsObj=paramsObj();
                        $paramsObj->params=array(
                            'dataType' => 'module',
                            'dataId' => $dataObj->request->moduleId,
                        );
                        $checkResult=app("S_Admin_category")->getCount($paramsObj);
                        if ($checkResult["error_no"] == 0) {
                            $categoryResult=$checkResult["result"];
                            $total_nums=$categoryResult["num"]>0?$categoryResult["num"]:0;

                            $temp_param=array();
                            $temp_param["total_nums"]=$total_nums;
                            $temp_param["start_nums"]=1;
                            $temp_param["categoryId"]=0;

                            $temp_return=array();
                            $temp_return["progress_msg"]="{$module['name']}列表开始更新";
                            $temp_return["referer"] = YIFANG_ROUTE_PREFIX."/cache/update?action=list";

                            $dataObj->data->temp_params=$temp_param;
                            $dataObj->data->temp_return=$temp_return;
                        } else {
                            $final_arr = logCallErrorMsg($final_arr, $checkResult);
                        }
                    }
                }
                if ($final_arr["error_no"] == 0) {
                    $params=array();
                    $params["all"]=$dataObj->request->all;
                    $params["moduleId"]=$dataObj->request->moduleId;
                    $params=array_merge($params,$dataObj->data->temp_params);

                    $return=array();
                    $return["params"]=$params;
                    $return=array_merge($return,$dataObj->data->temp_return);
                    $final_arr["result"]=$return;
                }
            }else{
                $params=array();
                $params["all"]=$dataObj->request->all;
                $params["moduleId"]=$dataObj->request->moduleId;

                $return=array();
                $return["params"]=$params;
                $return["progress_msg"]="{$module['name']}列表生成成功";
                if($dataObj->request->all=="1"){
                    $return["referer"] = YIFANG_ROUTE_PREFIX."/cache/update?action=show";
                }else{
                    $return["referer"] = YIFANG_ROUTE_PREFIX."/cache?moduleId=".$dataObj->request->moduleId;
                    $return["method"] = "get";
                }
                $final_arr["result"]=$return;
            }
        }
        return $final_arr;
    }

    /**
     * 更新信息
     * @param string $paramObj
     */
    public function module_show($paramObj = "")
    {
        $dataObj = paramsObj();
        $dataObj->request = paramsObj();
        $dataObj->data = paramsObj();
        $dataObj->data->use_trans = false;
        $dataObj->result = new \ArrayObject();

        $final_arr = logInit();
        if ($final_arr["error_no"] == 0) {
            $mixed = parseToArr($paramObj, "mixed", 0);
            $params = parseToArr($paramObj, "params", array());
            $messages = parseToArr($paramObj, "messages", array());
            $rules = array(
                'action' => 'require',
                'moduleId' => 'require',
                'update' => '',
                'fid' => '',
                'all' => '',
                'tid' => '',
                'sid' => '',
                'page' => '',
                'pageSize' => '',
            );
            $dataDefault=array();
            $dataDefault["update"]=0;
            $paramsObj = paramsObj();
            $paramsObj->params = $params;
            $paramsObj->mixed = $mixed;
            $paramsObj->rules = $rules;
            $paramsObj->messages = $messages;
            $paramsObj->dataDefault = $dataDefault;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $dataObj->request = $checkResult["result"];
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {
            if(!isset($dataObj->request->fid) || !isset($dataObj->request->tid) || !isset($dataObj->request->pageSize)){
                //获取最小ID
                $paramsObj=paramsObj();
                $paramsObj->params=array(
                    "moduleId"=>$dataObj->request->moduleId
                );
                $checkResult=$this->getPositionData($paramsObj);
                if ($checkResult["error_no"] == 0) {
                    $positionData = $checkResult["result"];
                    if(!isset($dataObj->request->fid)){
                        $dataObj->request->fid=$positionData["start_id"];
                    }
                    if(!isset($dataObj->request->tid)){
                        $dataObj->request->tid=$positionData["end_id"];
                    }
                    if(!isset($dataObj->request->pageSize)){
                        $dataObj->request->pageSize=$positionData["loop_nums"];
                    }
                } else {
                    $final_arr = logCallErrorMsg($final_arr, $checkResult);
                }
            }
        }
        if ($final_arr["error_no"] == 0) {
            if(!is_numeric($dataObj->request->sid)){
                $dataObj->request->sid=$dataObj->request->fid;
            }
        }
        if ($final_arr["error_no"] == 0) {
            $dataObj->data->pass=false;
            $index_html=setting("moduleConHtml",$dataObj->request->moduleId);
            if($index_html || $dataObj->request->update){
                $dataObj->data->pass=true;
            }
        }
        if ($final_arr["error_no"] == 0) {
            $module=module($dataObj->request->moduleId);
            if($dataObj->data->pass){
                if ($final_arr["error_no"] == 0) {
                    $dataObj->data->over=0;
                    if($dataObj->request->fid <= $dataObj->request->tid) {
                        $paramsObj=paramsObj();
                        $paramsObj->params=array(
                            "moduleId"=>$dataObj->request->moduleId,
                            'page' => $dataObj->request->page,
                            'pageSize' => $dataObj->request->pageSize,
                            'fid' => $dataObj->request->fid,
                        );
                        $checkResult=app("S_Admin_article")->getProccess($paramsObj);
                        if ($checkResult["error_no"] == 0) {
                            $result = $checkResult["result"];
                            if($result){
                                if($dataObj->request->update=="1"){
                                    $data=array();
                                    foreach($result as $val){
                                        $dataObj->data->itemid=$val["id"];
                                        $linkurl=itemurl($val);
                                        $data["id"][]=$val["id"];
                                        $data["data"][$val["id"]]=$linkurl;
                                    }
                                    $case="";
                                    foreach($data["id"] as $id){
                                        if(isset($data["data"][$id])){
                                            $case.="WHEN {$id} THEN '{$data["data"][$id]}' ";
                                        }
                                    }
                                    $tableName=app("cmsTable")::article($dataObj->request->moduleId);
                                    $sql="update `{$tableName}` set linkUrl = CASE id {$case} END where id IN(".implode(",",$data["id"]).")";
                                    Db()->execute($sql);
                                }else{
                                    foreach($result as $val){
                                        $dataObj->data->itemid=$val["id"];
                                        //生成缓存
                                        $paramsObj=paramsObj();
                                        $paramsObj->params=array(
                                            "articleData"=>$val,
                                        );
                                        $checkResult=app("L_Admin_html")->module_show($paramsObj);
                                        if ($checkResult["error_no"] == 0) {

                                        }else{
                                            $final_arr = logCallErrorMsg($final_arr, $checkResult);
                                        }
                                    }
                                }
                                $dataObj->data->itemid += 1;
                                if(count($result) < $dataObj->request->pageSize){
                                    $dataObj->data->over=1;
                                }
                            }else{
                                $dataObj->data->itemid=$dataObj->request->fid + $dataObj->request->pageSize;
                                $dataObj->data->over=1;
                            }
                        }else{
                            $final_arr = logCallErrorMsg($final_arr, $checkResult);
                        }
                    }else{
                        $dataObj->data->over=1;
                        $dataObj->data->itemid=$dataObj->request->fid + $dataObj->request->pageSize;
                    }
                }
                if ($final_arr["error_no"] == 0) {
                    $params=array();
                    $params["all"]=$dataObj->request->all;
                    $params["moduleId"]=$dataObj->request->moduleId;
                    if($dataObj->data->over=="0"){
                        $params["fid"]=$dataObj->data->itemid;
                        $params["tid"]=$dataObj->request->tid;
                        $params["sid"]=$dataObj->request->sid;
                        $params["pageSize"]=$dataObj->request->pageSize;
                        $params["page"]=$dataObj->request->page+1;
                    }
                    $return=array();
                    $return["params"]=$params;
                    $return["progress"]=$this->progress($dataObj->request->sid,$dataObj->request->fid,$dataObj->request->tid,$dataObj->data->over);
                    if($dataObj->request->update=="1"){
                        $msg="更新成功";
                    }else{
                        $msg="生成成功";
                    }
                    if($dataObj->request->fid <= $dataObj->request->tid) {
                        $return["progress_msg"]="ID 从 {$dataObj->request->fid} 至".($dataObj->data->itemid - 1)." {$module['name']}{$msg}";
                    }else{
                        $return["progress_msg"]="{$module['name']} {$msg}";
                    }
                    if($dataObj->data->over=="1"){
                        if($dataObj->request->all=="1" && $dataObj->request->update=="1"){
                            $return["referer"] = YIFANG_ROUTE_PREFIX."/cache/update?action=index";
                        }else{
                            $return["referer"] = YIFANG_ROUTE_PREFIX."/cache?moduleId=".$dataObj->request->moduleId;
                            $return["method"] = "get";
                        }
                    }else{
                        $return["referer"] = YIFANG_ROUTE_PREFIX."/cache/update?action={$dataObj->request->action}&update={$dataObj->request->update}";
                    }
                    $final_arr["result"]=$return;
                }
            }else{
                $params=array();
                $params["all"]=$dataObj->request->all;
                $params["moduleId"]=$dataObj->request->moduleId;

                $return=array();
                $return["params"]=$params;
                $return["progress_msg"]="{$module['name']} 生成成功";
                $return["referer"] = YIFANG_ROUTE_PREFIX."/cache?moduleId=".$dataObj->request->moduleId;
                $return["method"] = "get";
                $final_arr["result"]=$return;
            }
        }
        return $final_arr;
    }

    /**
     * 进度
     * @param $sid
     * @param $fid
     * @param $tid
     * @return int|string
     */
    public function progress($sid, $fid, $tid,$over) {
        if($tid > $sid && $fid < $tid && $over=="0") {
            $p = $this->dround(($fid-$sid)*100/($tid-$sid), 0, true);
            if($p > 100) $p = 100;
            if($p < 1) $p = 1;
            $p = $p.'%';
        } else {
            $p = '100%';
        }
        return $p;
    }

    public function dround($var, $precision = 2, $sprinft = false) {
        $var = round(floatval($var), $precision);
        if($sprinft) $var = sprintf('%.'.$precision.'f', $var);
        return $var;
    }
}